<template>
	<view class="content">

		<!-- 轮播图 -->
		<uni-swiper-dot :info="info" :current="current" :mode="mode" :dots-styles="dotsStyles" field="content">
			<swiper circular autoplay="true" interval="2000" duration="500" class="swiper-box" @change="change">
				<swiper-item v-for="(item, index) in info" :key="index">
					<view :class="item.colorClass">
						<image class="image" height="200px" :src="item.url" mode="aspectFill" />
					</view>
				</swiper-item>
			</swiper>
		</uni-swiper-dot>
		<!-- 轮播图 -->

		<!-- 菜单 -->
		<view class="cu-list grid" :class="['col-' + gridCol,gridBorder?'':'no-border']">
			<view @click="onGridClick(item.id)" class="cu-item" v-for="(item,index) in cuIconList" :key="index"
				v-if="index<gridCol*2">
				<view :class="['cuIcon-' + item.cuIcon,'text-' + item.color]">
					<view class="cu-tag badge" v-if="item.badge!=0">
						<block v-if="item.badge!=1">{{item.badge>99?'99+':item.badge}}</block>
					</view>
				</view>
				<text>{{item.name}}</text>
			</view>
		</view>
		<!-- 菜单 -->


	</view>
</template>

<script>
	//getApp().globalData.authnum=3
	export default {

		data() {
			return {
				//轮播图
				info: [{
						colorClass: 'uni-bg-red',

						url: 'https://hafcapp.oss-cn-shanghai.aliyuncs.com/cs/187550838.jpg?Expires=3793629600&OSSAccessKeyId=LTAIA9VLEk1xfvBJ&Signature=pfM%2BXZTdRRErkNOx0Eh8R3prjis%3D',
						content: '内容 A'
					},
					{
						colorClass: 'uni-bg-green',
						url: 'https://hafcapp.oss-cn-shanghai.aliyuncs.com/cs/694574474.jpg?Expires=3793629600&OSSAccessKeyId=LTAIA9VLEk1xfvBJ&Signature=TsmiN394hNGA5d4XVXvPuyFG9Xg%3D',
						content: '内容 B'
					},
					{
						colorClass: 'uni-bg-blue',
						url: 'https://hafcapp.oss-cn-shanghai.aliyuncs.com/cs/328478142.jpg?Expires=3793629600&OSSAccessKeyId=LTAIA9VLEk1xfvBJ&Signature=9V4DFNKCt8miE9d6A25lqc%2BfKW8%3D',
						content: '内容 C'
					}
				],
				dotStyle: [{
						backgroundColor: 'rgba(0, 0, 0, .3)',
						border: '1px rgba(0, 0, 0, .3) solid',
						color: '#fff',
						selectedBackgroundColor: 'rgba(0, 0, 0, .9)',
						selectedBorder: '1px rgba(0, 0, 0, .9) solid'
					},
					{
						backgroundColor: 'rgba(255, 90, 95,0.3)',
						border: '1px rgba(255, 90, 95,0.3) solid',
						color: '#fff',
						selectedBackgroundColor: 'rgba(255, 90, 95,0.9)',
						selectedBorder: '1px rgba(255, 90, 95,0.9) solid'
					},
					{
						backgroundColor: 'rgba(83, 200, 249,0.3)',
						border: '1px rgba(83, 200, 249,0.3) solid',
						color: '#fff',
						selectedBackgroundColor: 'rgba(83, 200, 249,0.9)',
						selectedBorder: '1px rgba(83, 200, 249,0.9) solid'
					}
				],
				modeIndex: -1,
				styleIndex: -1,
				current: 0,
				mode: 'default',
				dotsStyles: {},
				//菜单
				cuIconList: [{
						cuIcon: 'repairfill',
						color: 'olive',
						badge: getApp().globalData.authnum,
						name: '授权',
						id: 'auth'
					},
					{
						cuIcon: 'roundclosefill',
						color: 'red',
						badge: 0,
						name: '清除缓存',
						id: 'roundclosefill'
					}

					// {
					// 	cuIcon: 'formfill',
					// 	color: 'yellow',
					// 	badge: 0,
					// 	name: '外出'
					// }, {
					// 	cuIcon: 'clothesfill',
					// 	color: 'green',
					// 	badge: 0,
					// 	name: '加班',
					// 	id: 'devicelist'
					// },
				],
				gridCol: 3,
				gridBorder: false

			};
		},
		onShow() {
			// uni.setTabBarItem({
			// 	index: 0,
			// 	text: this.$t('home')
			// })
			// uni.setTabBarItem({
			// 	index: 1,
			// 	text: this.$t('mine')
			// })
			// this.getMessage()
		},
		onLoad() {

			/**
			 * 加载授权列表的数量
			 */


			uni.request({
				url: this.$constant.BASEURL + this.$constant.GET_AUTHLIST,
				method: "POST",
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				},
				// data: {
				// 	username: getApp().globalData.username,
				// 	token: getApp().globalData.token,
				// 	rec: 2,
				// 	departid: getApp().globalData.departid
				// },
				complete: (res) => {
					console.log(res.data)
					if (res.data.code == 0) {
						getApp().globalData.authnum = res.data.data.length;
						console.log('产品数量' + getApp().globalData.authnum)
						this.cuIconList[0].badge = getApp().globalData.authnum;
					} else {
						new Promise(function(resolve, reject) {
							uni.showToast({
								title: res.data.message,
								icon: 'none',
							})
							setTimeout(function() {
								resolve()
							}, 2000)
						}).then(function() {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						})
					}
				}
			});



		},
		onLaunch() {



		},
		methods: {
			// getMessage() {
			// 	uni.request({
			// 		url: this.$constant.BASEURL + this.$constant.GET_MESSAGE_TOTAL,
			// 		method: "POST",
			// 		header: {
			// 			'content-type': 'application/x-www-form-urlencoded'
			// 		},
			// 		data: {
			// 			userid: getApp().globalData.userid,
			// 		},
			// 		complete: (res) => {
			// 			console.log(res.data)
			// 			if (res.data.code == 0) {
			// 				//重新刷新数据
			// 				let result = res.data.data
			// 				this.cuIconList[3].badge = result.stay
			// 			}
			// 		}
			// 	})
			// },
			change(e) {
				this.current = e.detail.current
			},
			onGridClick(id) {
				// console.log(id)
				switch (id) {
					case 'leave':
						uni.navigateTo({
							url: 'leave/leavetype'
						})
						break
					case 'auth':
						console.log("跳转授权列表")
						// uni.navigateTo({
						// 	url: '/pages/plugin/indexesnew'
						// })
						uni.reLaunch({
							url: '/pages/plugin/indexesnew',
							success: function() {
								console.log('跳转成功');
							},
							fail: function(err) {
								console.log('跳转失败:', err);
							}
						});
						break
					case 'roundclosefill':
						uni.clearStorage();
						//关闭当前网页  顺序不能颠倒 同时兼容ios和android
						// document.addEventListener('WeixinJSBridgeReady', function() {
						// 	WeixinJSBridge.call('closeWindow');
						// }, false);
						// WeixinJSBridge.call('closeWindow');

						// 拦截路由，并且跳转去其他页面
						new Promise(function(resolve, reject) {
							//当异步代码执行成功时，我们才会调用resolve(...), 当异步代码失败时就会调用reject(...)
							//在本例中，我们使用setTimeout(...)来模拟异步代码，实际编码时可能是XHR请求或是HTML5的一些API方法.
							// uni.showToast({
							// 	title: '清除缓存成功！',
							// 	icon: 'none'
							// })
							uni.showModal({
								title: "提示",
								content: "清除缓存成功，请重新登录！",
								showCancel: false,
								success() {
									console.log("logout success!")
								}
							})
							setTimeout(function(){
								resolve('/pages/login/login'); //代码正常执行！
							}, 1000);
						}).then(function(url) {
							uni.navigateTo({
								url: url
							})
						});;

						break
					case 'businessTravel':
						uni.navigateTo({
							url: 'business-travel/business-travel'
						})
						break
					default:
						uni.showToast({
							title: this.$t('comingSoon'),
							icon: 'none'
						})
						break;
				}
			}
		}
	}
</script>

<style>
	.swiper-box {
		height: 200px;
	}

	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background-color: #999;
		color: #fff;
	}

	.image {
		width: 750rpx;
	}
</style>